package com.tencent.angel.utils;

import com.tencent.angel.RunningMode;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import org.apache.commons.logging.Log;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.api.records.Container;
import org.apache.hadoop.yarn.api.records.LocalResource;
import org.apache.hadoop.yarn.api.records.LocalResourceType;
import org.apache.hadoop.yarn.api.records.LocalResourceVisibility;
import org.apache.hadoop.yarn.api.records.URL;
import org.apache.hadoop.yarn.util.ConverterUtils;

/**
 * @author stevenschew@apache.org
 */
public class CommonUtil {
    public static RunningMode translateToRunningMode(String mode) {
        if (mode.equals(RunningMode.ANGEL_PS.toString())) {
            return RunningMode.ANGEL_PS;
        } else if (mode.equals(RunningMode.ANGEL_PS_PSAGENT.toString())) {
            return RunningMode.ANGEL_PS_PSAGENT;
        } else {
            return RunningMode.ANGEL_PS_WORKER;
        }
    }

    public static List<String> getDiagnostics(Lock readLock, List<String> diagnostics) {
        try {
            readLock.lock();
            List<String> cloneDiagnostics = new ArrayList<>(diagnostics.size());
            cloneDiagnostics.addAll(diagnostics);
            return cloneDiagnostics;
        } finally {
            readLock.unlock();
        }
    }

    public static void serviceStop(Thread timeOutChecker, Log LOG) {
        if (timeOutChecker != null) {
            timeOutChecker.interrupt();
            try {
                timeOutChecker.join();
            } catch (InterruptedException ie) {
                LOG.warn("InterruptedException while stopping", ie);
            }
            timeOutChecker = null;
        }
    }

    public static LocalResource createLocalResource(FileSystem fc, Path file,
        LocalResourceType type, LocalResourceVisibility visibility) throws IOException {
        FileStatus fstat = fc.getFileStatus(file);
        URL resourceURL = ConverterUtils.getYarnUrlFromPath(fc.resolvePath(fstat.getPath()));
        long resourceSize = fstat.getLen();
        long resourceModificationTime = fstat.getModificationTime();

        return LocalResource.newInstance(resourceURL, type, visibility, resourceSize,
            resourceModificationTime);
    }

    public static String getNodeAddr(Container container, Lock readLock) {
        try {
            readLock.lock();
            if (container == null) {
                return null;
            } else {
                return container.getNodeHttpAddress();
            }
        } finally {
            readLock.unlock();
        }
    }

    public static String getContainerIdStr(Container container, Lock readLock) {
        try {
            readLock.lock();
            if (container == null) {
                return null;
            } else {
                return container.getId().toString();
            }
        } finally {
            readLock.unlock();
        }
    }

    public static Map<String, String> getMetrics(Map<String, String> metrics, Lock readLock) {
        try {
            readLock.lock();
            Map<String, String> cloneMetrics = new HashMap<>();
            cloneMetrics.putAll(metrics);
            return cloneMetrics;
        } finally {
            readLock.unlock();
        }
    }

}
